package cn.krund.lyc.hotlerobot.activity.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import java.util.Stack;

import butterknife.ButterKnife;

/**
 * @包名: cn.krund.lyc.hotlerobot.activity.base
 * @作者: 刘育成
 * @时间: 2018/3/21  14:28
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: BaseActivity
 */

public abstract class BaseActivity extends Activity {

    /**
     * 用来保存所有已打开的Activity
     */
    private static Stack<Activity> listActivity = new Stack<Activity>();

    private Context mContext;

    /**
     * 按钮连续点击最低间隔时间 单位：毫秒
     **/
    public final static int CLICK_TIME = 1000;
    /**
     * 记录上次点击按钮的时间
     **/
    private long lastClickTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mContext = this;
        // 将activity推入栈中
        listActivity.push(this);
        initView();
        initNev();
        setActivityState(mContext);
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    public Context getContext() {
        return this.mContext;
    }

    /**
     * 设置屏幕只能横屏
     *
     * @param context
     */

    public void setActivityState(Context context) {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }


    /**
     * @return 布局文件
     */
    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initNev();

    @Override
    protected void onResume() {

        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        saveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    /**
     * 保存activity状态
     **/
    protected void saveInstanceState(Bundle outState) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 从栈中移除当前activity
        if (listActivity.contains(this)) {
            listActivity.remove(this);
        }

    }


    // 封装跳转
    public void openActivity(Class<?> c) {
        openActivity(c, null);
    }

    // 跳转 传递数据 bundel
    public void openActivity(Class<?> c, Bundle b) {
        openActivity(c, b, null);
    }

    public void openActivity(Class<?> c, Bundle b, Uri uri) {
        Intent intent = new Intent(this, c);
        if (b != null) {
            intent.putExtras(b);
        }
        if (uri != null) {
            intent.setData(uri);
        }

        startActivity(intent);

    }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 验证上次点击按钮时间间隔，防止重复点击
     */
    public boolean verifyClickTime() {
        if (System.currentTimeMillis() - lastClickTime <= CLICK_TIME) {
            return false;
        }
        lastClickTime = System.currentTimeMillis();
        return true;
    }

    /**
     * 关闭所有(前台、后台)Activity,注意：请已BaseActivity为父类
     */
    protected static void finishAll() {
        int len = listActivity.size();
        for (int i = 0; i < len; i++) {
            Activity activity = listActivity.pop();
            activity.finish();
        }
    }
}
